//
//  FlightResultDetailController.m
//  ELife
//
//  Created by xiaozhi on 11-5-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "FlightResultDetailController.h"
#import "StepView.h"
#import "FlightModel.h"
#import "UIViewUtil.h"
#import "FlightBookController.h"
#import "FlightSearchController.h"
#import "FlightCheckedModel.h"

@implementation FlightResultDetailController
@synthesize scrollView;
NSInteger _detailY;
NSString *_fromAirportName;
NSString *_arriveAirportName;
NSString *_flightDate; // 从哪机场到哪机场 日期多少


- (void)buildDetailLabelWithStr:(NSString *)text 
                    parentFrame:(CGRect)parentFrame
{
    CGFloat OFFSET_X = 15;
    CGFloat OFFSET_Y = 5;
    CGFloat x = parentFrame.origin.x + OFFSET_X;
    CGFloat CHILD_WID = parentFrame.size.width - OFFSET_X * 2;
    
    UIFont *font = [UIFont boldSystemFontOfSize:14];
    CGSize fontSize = [text sizeWithFont:font];
    
    // 计算label行数
    NSInteger lineNum = fontSize.width / CHILD_WID;
    if ((NSInteger)fontSize.width % (NSInteger)CHILD_WID > 0) {
        lineNum++;
    }
    
    CGFloat childHei = fontSize.height * lineNum;
    
    // create label
    UILabel *label = [[UILabel alloc]initWithFrame:
                      CGRectMake(x, _detailY, CHILD_WID, childHei)];
    label.numberOfLines = lineNum;
    label.font = font;
    label.text = text;
    
    // add to container
    [scrollView addSubview:label];
    [label release];
    
    // 增加y偏移
    _detailY += (childHei + OFFSET_Y);
}

- (NSString *)formatHourTime:(NSString *)date
{
    return [date substringFromIndex:[date length] - 6];
}

- (void)fillDetail:(FlightDetailModel *)model
{
//    <airline><AirCompany>3U</AirCompany>
//    <FlightNO>3U8732</FlightNO>
//    <PlaneStyle>320</PlaneStyle>
//    <Mile>1390</Mile>
//    <BasePrice>1300</BasePrice>
//    <Discount>100</Discount>
//    <AirportTax>50</AirportTax>
//    <FuelTax>70</FuelTax>
//    <IsStop>0</IsStop>
//    <DepDateTime>2011-03-15 11:55</DepDateTime>
//    <ArriDateTime>2011-03-15 14:15</ArriDateTime></airline>

    [self.view viewWithTag:TAG_DETAIL_CONTAINER].layer.cornerRadius = 8;
    [self.view viewWithTag:TAG_DETAIL_CONTAINER].layer.masksToBounds= YES;
    
    CGRect containerRect = scrollView.frame;
    _detailY = containerRect.origin.y + 5;
    
    
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"%@-%@ (%@)", _fromAirportName, _arriveAirportName, _flightDate] parentFrame:containerRect];
    
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"航班/机型:%@/%@:", model.flightNO, model.planeStyle] parentFrame:containerRect];
//    [self buildDetailLabelWithStr:[NSString stringWithFormat:(NSString *), ...]@"舱位等级:经济舱" 
//                      parentFrame:containerRect];
    
    NSString *hourTime = [self formatHourTime:model.depDateTime];
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"起飞:%@ %@", hourTime, _fromAirportName] parentFrame:containerRect];
    
    hourTime = [self formatHourTime:model.arriDateTime];
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"到达:%@ %@", hourTime, _arriveAirportName] parentFrame:containerRect];
    
    [self buildDetailLabelWithStr:@"更改条件 可以更改" 
                      parentFrame:containerRect];
    [self buildDetailLabelWithStr:
     @"退票条件: 需收取票面价5%的退票费(婴儿或儿童免收退票费)"
                      parentFrame:containerRect];
    [self buildDetailLabelWithStr:@"签约条件:不得转签"
                      parentFrame:containerRect];
    [self buildDetailLabelWithStr:@"备注:直减优惠限邮寄或北京取票儿童加CHD"
                      parentFrame:containerRect];
    
    NSInteger price = [model.basePrice intValue];
    NSInteger discount = [model.discount intValue];
    NSInteger formerlyPrice = price * discount / 100;
    double discountDoubleValue = (double)(discount / 10);
    NSString *discountStr = @"无折扣";
    if (discount < 100) {
       discountStr = [[NSString stringWithFormat:@"%f 折", discountDoubleValue] substringToIndex:3];
    }
    
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"机票价格:%d元(原价:%d元 %@)",formerlyPrice, price, discountStr] parentFrame:containerRect];
    
    NSInteger airportTax = [model.airportTax intValue];
    NSInteger fuelTax = [model.fuelTax intValue];
    
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"机建/燃油:%d元/%d元", airportTax, fuelTax] parentFrame:containerRect];
    
    NSInteger totalPrice = price + airportTax + fuelTax;
    [self buildDetailLabelWithStr:[NSString stringWithFormat:@"单人总价:%d元", totalPrice] 
                      parentFrame:containerRect];
    
    // set total
    [FlightSearchController getCheckedModel].totalPrice = [NSString stringWithFormat:@"%d", totalPrice];
    [scrollView setContentSize:CGSizeMake(containerRect.size.width, _detailY)];
}

- (void)setFlightList:(FlightDetailModel *)flightModel 
      withFromAirport:(NSString *)fromAirport
    withArriveAirport:(NSString *)arriveAirport
             withDate:(NSString *)date;
{
    FlightCheckedModel *model = [FlightSearchController getCheckedModel];
    model.fromAirport = fromAirport;
    model.arriveAirport = arriveAirport;
    model.detailModel = flightModel;
    flightModel.date = date;
    
    _fromAirportName = [fromAirport retain];
    _arriveAirportName = [arriveAirport retain];
    
    [date retain];
    [_flightDate release];
    _flightDate = date;
        
    [self fillDetail:flightModel];
}

- (void)dealloc
{
    [_fromAirportName release];
    [_arriveAirportName release];
    [_flightDate release];
    [scrollView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)initStepView
{
    StepView *stepView = [[StepView alloc] createStepViewWithStep:1];
    [self.view addSubview:stepView];
    [stepView release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIViewUtil getCommonBg];
    [self initStepView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)bookAction
{
    FlightBookController *nextController = [[FlightBookController alloc]initWithNibName:@"FlightBookController" bundle:nil];
    [self.navigationController pushViewController:nextController animated:YES];
    [nextController release];
}

@end
